package com.server.core.server;

import com.server.core.init.DefautInitializet;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

public class NettyServer implements Runnable{

    public void run() {
        ServerBootstrap bootstrap = new ServerBootstrap();
        ///创建2个线程池，boss用于接待新来的请求，worker用于处理请求
        NioEventLoopGroup boss = new NioEventLoopGroup(1);
        NioEventLoopGroup worker = new NioEventLoopGroup();
        try{
            ////设置线程池
            bootstrap.group(boss, worker);
            ////设置通道
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.childHandler(
                    new DefautInitializet()
            );
            ChannelFuture future = bootstrap.bind(8080).sync();
            future.addListener(new GenericFutureListener<Future<? super Void>>() {
                @Override
                public void operationComplete(Future<? super Void> future) throws Exception {
                     System.out.println("--------------------------------\n" +
                                        "------    服务器启动成功    ------\n" +
                                        "------    Hello  World    ------\n" +
                                        "--------------------------------");
                }
            });
        }catch (Exception e) {
                e.printStackTrace();
                boss.shutdownGracefully();
                worker.shutdownGracefully();
        }
        finally {

        }
    }

}
